package com.eskie.vmobileapp;

import java.lang.reflect.Field;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

public class ReloadActivity extends SherlockActivity {
	private DrawerLayout drawerLayout;
	private ExpandableListView drawerListView;
	private ActionBarDrawerToggle actionBarDrawerToggle;
	private TextView titleTV;
	Field mDragger;
	int edge;
	Field mEdgeSize;
	ViewDragHelper draggerObj;
	EditText txtAmount, txtCode, txtMonth, txtDay, txtYear, txtHour, txtMinute,
			txtSecond;
	Button btnSend;
	RadioButton RbtnBPI, RbtnBDO, RbtnMetro;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reload);
		// Changing the title of the action bar
		LayoutInflater inflater = LayoutInflater.from(this);
		View customView = inflater.inflate(R.layout.title_load, null);
		titleTV = (TextView) customView.findViewById(R.id.title_load);
		titleTV.setText("Reload");
		getSupportActionBar().setCustomView(customView);
		getSupportActionBar().setDisplayShowCustomEnabled(true);

		txtAmount = (EditText) findViewById(R.id.txtRAmount);
		txtCode = (EditText) findViewById(R.id.txtBranch);
		txtMonth = (EditText) findViewById(R.id.txtMonth);
		txtDay = (EditText) findViewById(R.id.txtDay);
		txtYear = (EditText) findViewById(R.id.txtYear);
		txtHour = (EditText) findViewById(R.id.txtHour);
		txtMinute = (EditText) findViewById(R.id.txtMinute);
		txtSecond = (EditText) findViewById(R.id.txtSecond);

		btnSend = (Button) findViewById(R.id.btnRSend);

		RbtnBDO = (RadioButton) findViewById(R.id.rdBDO);
		RbtnBPI = (RadioButton) findViewById(R.id.rdBPI);
		RbtnMetro = (RadioButton) findViewById(R.id.rdMetro);
		
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (txtAmount.getText().toString().equals("") || txtCode.getText().toString().equals("")
						|| txtDay.getText().toString().equals("") || txtHour.getText().toString().equals("")
						|| txtMinute.getText().toString().equals("") || txtMonth.getText().toString().equals("")
						|| txtSecond.getText().toString().equals("") || txtYear.getText().toString().equals("")) {
					Toast.makeText(getApplicationContext(),
							"Fill up the blanks", Toast.LENGTH_SHORT).show();
				} else {
					GlobalVar.amount = txtAmount.getText().toString();
					GlobalVar.branchCode = txtCode.getText().toString();
					GlobalVar.month = txtMinute.getText().toString();
					GlobalVar.day = txtDay.getText().toString();
					GlobalVar.year = txtYear.getText().toString();
					GlobalVar.hour = txtHour.getText().toString();
					GlobalVar.minute = txtMinute.getText().toString();
					GlobalVar.second = txtSecond.getText().toString();
					GlobalVar.menu = "Reload";
					if (RbtnBDO.isChecked())
						GlobalVar.BankName = "BDO";
					else if (RbtnBPI.isChecked())
						GlobalVar.BankName= "BPI";
					else
						GlobalVar.BankName = "Metro";
					new CustomDialog(v.getContext(), CustomDialog.PIN).start();
				}
			}
		});
		

		// get ListView defined in activity_main.xml
		drawerListView = (ExpandableListView) findViewById(R.id.left_drawer);

		Drawer_List d = new Drawer_List("Reload");

		// Set the adapter for the list view
		final ExpandableDrawerAdapter expListAdapter = new ExpandableDrawerAdapter(
				this, d.listDataHeader, d.listDataChild);
		drawerListView.setAdapter(expListAdapter);
		drawerListView.setGroupIndicator(null);

		// App Icon
		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
		drawerLayout, /* DrawerLayout object */
		R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
		R.string.drawer_open, /* "open drawer" description */
		R.string.drawer_close /* "close drawer" description */
		);

		drawerTouch();

		// Set actionBarDrawerToggle as the DrawerListener
		drawerLayout.setDrawerListener(actionBarDrawerToggle);

		// Enabling the action bar
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setLogo(R.drawable.ic_drawer);

		// just styling option add shadow the right edge of the drawer
		drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);

		// When the user click the Generate ID
		drawerListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				if (groupPosition == 1) {
					Toast.makeText(getApplicationContext(), "Feature not yet available",
							Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});

		
	}

	/**
	 * This method makes the drawer can be open through swipe anywhere in the
	 * Fragment
	 */
	private void drawerTouch() {
		try {
			mDragger = drawerLayout.getClass().getDeclaredField("mLeftDragger");
		} catch (NoSuchFieldException e) {
		}// mRightDragger for right obviously
		mDragger.setAccessible(true);

		try {
			draggerObj = (ViewDragHelper) mDragger.get(drawerLayout);
		} catch (IllegalArgumentException e) {
		} catch (IllegalAccessException e) {
		}

		try {
			mEdgeSize = draggerObj.getClass().getDeclaredField("mEdgeSize");
		} catch (NoSuchFieldException e) {
		}
		mEdgeSize.setAccessible(true);

		try {
			edge = mEdgeSize.getInt(draggerObj);
		} catch (IllegalArgumentException e) {
		} catch (IllegalAccessException e) {
		}
		try {
			mEdgeSize.setInt(draggerObj, edge * 8);
		} catch (IllegalArgumentException e) {
		} catch (IllegalAccessException e) {
		}
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		actionBarDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		actionBarDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.getItemId() == android.R.id.home) {
			if (drawerLayout.isDrawerOpen(drawerListView)) {
				drawerLayout.closeDrawer(drawerListView);
			} else {
				drawerLayout.openDrawer(drawerListView);
			}
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
